package com.msb.consumer.controller;

import com.alibaba.fastjson2.JSONObject;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author DreamAndLove
 * @date 2024/09/04 9:35
 * @description
 */
@RestController
public class TestController {

    @Autowired
    DiscoveryClient client;

    @Autowired
    EurekaClient eurekaClient;

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/client")
    public String client() {
        List<String> services = client.getServices();
        for (String service : services) {
            System.out.println(service);
        }
        return "client";
    }

    @GetMapping("/client1")
    public Object client1() {
        return client.getInstances("provider");
    }

    @GetMapping("/client2")
    public String client2() {
        // 具体服务
        //List<InstanceInfo> instances = eurekaClient.getInstancesById("localhost:provider:80");
        // 使用服务名称找列表
        List<InstanceInfo> instances = eurekaClient.getInstancesByVipAddress("provider", false);
        if(instances.size() > 0) {
            InstanceInfo info = instances.get(0);
            if (info.getStatus() == InstanceInfo.InstanceStatus.UP) {
                String url = "http://" + info.getHostName() + ":" + info.getPort() + "/sayHello";
                System.out.println(url);
                RestTemplate restTemplate = new RestTemplate();
                String forObject = restTemplate.getForObject(url, String.class);
                return "rest call result " + forObject;
            }
        }
        return "hello1";
    }

    @GetMapping("/client3")
    public String client3() {

        // 从provider这个服务的众多实例中选取一个实例返回，从而实现远程服务调用
        // choose方法中包含了负载均衡策略，它过滤掉down掉的实例
        ServiceInstance instance = loadBalancerClient.choose("provider");
        if(instance != null) {
            String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/sayHello";
            RestTemplate restTemplate = new RestTemplate();
            String forObject = restTemplate.getForObject(url, String.class);
            return "port:"+instance.getPort()+"--client3 rest call result: " + forObject;
        }
        return "hello3";
    }

    @GetMapping("/client4")
    public String client4() {

        // 从provider这个服务的众多实例中选取一个实例返回，从而实现远程服务调用
        // choose方法中包含了负载均衡策略，它过滤掉down掉的实例
        String url = "http://provider/sayHello";
        String forObject = restTemplate.getForObject(url, String.class);
        return "port:"+"--" + forObject;
    }

    /**
     * 功能:演示restTemplate传参
     * @author DreamAndLove
     * @return String
     * @date 2024/9/5 15:20
     */
    @GetMapping("/client5")
    public String client5() {

        String url = "http://provider/personInfo?name={1}&addr={2}";

        JSONObject forObject = restTemplate.getForObject(url, JSONObject.class, "张三","China");
        return "port:"+"--" + forObject.toString();
    }

    @GetMapping("/client6")
    public String client6() {
        /*
         * 演示传参，实际上该方法调用不通，provider没写相应的方法，只做演示
         */
        String url = "http://provider/sayHello?name={name}";
        Map<String, String> map = Collections.singletonMap("name", "zhangsan");

        String forObject = restTemplate.getForObject(url, String.class, map);
        return "port:"+"--" + forObject;
    }
}
